package com.qf.vote2203.dbvote.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.vote2203.dbvote.entity.Vote;
import com.qf.vote2203.dbvote.service.IVoteService;
import com.qf.vote2203.dbvote.websocket.PushStaticWebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class PushStatisticTask {

    @Autowired
    IVoteService voteService;

    @Scheduled(cron = "0/5 * * * * ?")
    public void pushData() throws IOException {

        if (PushStaticWebSocketServer.map.size()>0) {
            // 1 查询汇总人次
            final Object obj = voteService.getObj(new QueryWrapper<Vote>().select("sum(vote_num)"), o -> o);

            // 2 调用 websocket server提供的方法推送数据
            PushStaticWebSocketServer.sendMsg(obj.toString());
        }


    }

}
